<?php

namespace App\Http\Controllers;

use App\Http\Models\User;
use Illuminate\Http\Request;
use Gregwar\Captcha\CaptchaBuilder;
use Gregwar\Captcha\PhraseBuilder;

class UserController extends Controller
{
	/**
	 * 图片验证码
	 *
	 * @return \Symfony\Component\HttpFoundation\StreamedResponse
	 */
	public function getCaptcha()
	{
		$phrase = new PhraseBuilder;
		$code = $phrase->build(4);// 设置验证码位数
		$builder = new CaptchaBuilder($code, $phrase);// 生成验证码图片的Builder对象,配置相应属性
		$builder->setBackgroundColor(255, 255, 255);// 设置背景颜色
		$builder->setMaxAngle(25);// 设置倾斜角度
		$builder->setMaxBehindLines(10);// 设置验证码后面最大行数
		$builder->setMaxFrontLines(10);// 设置验证码前面最大行数
		$builder->setTextColor(78, 115, 223);// 设置验证码颜色
		$builder->build($width = 90, $height = 30, $font = null);// 可以设置图片宽高及字体
		$phrase = $builder->getPhrase();// 获取验证码的内容
		session()->put('captcha', $phrase);// 把内容存入session
		// 生成图片
		$builder->output();
		return response()->stream(function () use ($builder) {
			echo $builder->getContents();
		}, 200, [
			'Content-Type' => 'image/jpeg',
			'Cache-Control' => 'no-cache, must-revalidate'
		]);
	}

	/**
	 * login.
	 *
	 * @param Request $request
	 * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
	 */
	public function login(Request $request)
	{
		if (strcasecmp(session()->get('captcha'), $request->input('captcha')) == 0) {
			//验证登录
			$user = User::find($request->input('id'))->first();
			if ($user != null && $user['password'] == $request->input('password')) {
				session()->forget('captcha');
				session()->put('id', $user['id']);
				session()->put('name', $user['name']);
				session()->put('role', $user['role']);
				return $this->success($user);
			} else {
				// 登录失败后的相关操作
				return $this->failed("账号或密码错误");
			}
		} else {
			return $this->failed('验证码错误');
		}
	}

	/**
	 * logout
	 *
	 * @return  \Illuminate\Http\JsonResponse
	 */
	public function end()
	{
		session()->flush();
		return $this->success();
	}


	/**
	 * Display a listing of the resource.
	 *
	 * @param Request $request
	 * @return  \Illuminate\Http\JsonResponse
	 */
	public function changePassword(Request $request)
	{
		$user = User::find(session()->get('id'));
		if ($user['password'] == $request->input('old_password')) {
			$user->password = $request->input('new_password');
			return $user->save() ?
				$this->success('') : $this->failed('修改成功');
		} else {
			return $this->failed('原密码不匹配！');
		}
	}

	/**
	 * Display a listing of the resource.
	 *
	 * @param Request $request
	 * @return  \Illuminate\Http\JsonResponse
	 */
	public function index(Request $request)
	{
		if ($request->has('limit')) {
			$items = User::paginate($request->input('limit'));
		} else {
			$items = User::all();
		}

		return $this->table($items);
	}

	/**
	 * Store a newly created resource in storage.
	 *
	 * @param \Illuminate\Http\Request $request
	 * @return  \Illuminate\Http\JsonResponse
	 */
	public function store(Request $request)
	{
		$model = new User;
		foreach ($request->toArray() as $key => $value) {
			if (in_array($key, $model->getFillable())) {
				$model[$key] = $value;
			}
		}
		return $model->save() ?
			$this->success('') : $this->failed('添加失败');
	}

	/**
	 * Display the specified resource.
	 *
	 * @param int $id
	 * @return  \Illuminate\Http\JsonResponse
	 */
	public function show($id)
	{
		return $this->success(User::find($id));
	}

	/**
	 * Update the specified resource in storage.
	 *
	 * @param \Illuminate\Http\Request $request
	 * @param int $id
	 * @return  \Illuminate\Http\JsonResponse
	 */
	public function update(Request $request, $id)
	{
		$model = User::find($id);
		foreach ($request->toArray() as $key => $value) {
			if (in_array($key, $model->getFillable())) {
				$model[$key] = $value;
			}
		}
		return $model->update() ? $this->success('') : $this->failed('更新失败');
	}

	/**
	 * Remove the specified resource from storage.
	 *
	 * @param int $id
	 * @return  \Illuminate\Http\JsonResponse
	 */
	public function destroy($id)
	{
		return User::where('id', $id)->delete() ? $this->success('') : $this->failed('删除失败');

	}
}
